package com.tomtop.core.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class ExceptionController {

	@RequestMapping("/exception/notfound") ModelAndView notfound(HttpServletRequest request, HttpServletResponse response ){
		GlobalException notfound = new GlobalException(GlobalExceptionStatus.PAGE_NOT_FOUND);
		return GlobalApiExceptionHandler.outException(request, response,notfound);
	}

	@RequestMapping("/exception/server-error") ModelAndView serverError(HttpServletRequest request, HttpServletResponse response){
		Exception ex = (Exception) request.getAttribute("javax.servlet.error.exception");
		String errorMsg = (ex == null || StringUtils.isEmpty(ex.getMessage())) ? "so sorry,error occured." : ex.getMessage();
		GlobalException customException= new GlobalException(GlobalExceptionStatus.FAIL, ex,errorMsg);
		return GlobalApiExceptionHandler.outException(request, response,customException);
	}
}
